package org.onvivals.app.modules.ums.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.onvivals.app.modules.auth.util.SaTokenUtil;
import org.onvivals.app.modules.ums.param.UserEditParam;
import org.onvivals.common.web.Result;
import org.onvivals.module.ums.entity.UmsUser;
import org.onvivals.module.ums.service.IUmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Tag(name = "小程序:个人信息")
@RequestMapping("/user")
public class UmsUserController {

	private final IUmsUserService umsUserService;

	/**
	 * 修改个人信息
	 */
	@PostMapping("/edit")
	@Operation(summary = "用户修改信息")
	public Result<Object> edit(@Validated @RequestBody UserEditParam param) {
		UmsUser member = SaTokenUtil.getUser();
		if (member == null) {
			return Result.error("用户不存在");
		}
		if (StrUtil.isNotEmpty(param.getAvatar())) {
			member.setAvatar(param.getAvatar());
		}
		if (StrUtil.isNotEmpty(param.getNickname())) {
			member.setNickname(param.getNickname());
		}
		if (StrUtil.isNotEmpty(param.getGender())) {
			member.setGender(param.getGender());
		}
		if (StrUtil.isNotEmpty(param.getBirthday())) {
			member.setBirthday(param.getBirthday());
		}
		if (StrUtil.isNotEmpty(param.getIntroduction())) {
			member.setIntroduction(param.getIntroduction());
		}
		umsUserService.updateById(member);
		return Result.ok("修改成功");
	}
}
